package com.ctshk.task.worker.processors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.dto.Result;
import com.ctshk.rpc.visa.service.IVisaOrderTaskService;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * TODO
 *
 * @author huangxiansheng
 * @date 2021/5/18 1:43 下午
 */
@Slf4j
@Component
public class OrderVisaExpiresProcessor implements BasicProcessor {

    @DubboReference
    private IVisaOrderTaskService iVisaOrderTaskService;


    @Override
    public ProcessResult process(TaskContext context) throws Exception {
        OmsLogger omsLogger = context.getOmsLogger();
        JSONObject jsonObject = JSON.parseObject(context.getInstanceParams());
        Long orderId = jsonObject.getLong("id");
        Result result = iVisaOrderTaskService.notPaid(orderId);
        if (!result.isSuccess()) {
            omsLogger.error("订单设置未付款状态操作失败");
        } else {
            omsLogger.info("订单设置未付款状态操作成功");
        }
        // 根据控制台参数判断是否成功
        boolean success = !"failed".equals(context.getJobParams());
        omsLogger.info("OrderVisaExpiresProcessor finished process,success: .", success);

        return new ProcessResult(success, context + ": " + success);
    }
}
